import 'package:bilibili_getx_project/core/I18n/str_res_keys.dart';
import 'package:bilibili_getx_project/ui/shared/app_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../shared/asset_image.dart';
import 'home_logic.dart';

class HomeView extends StatefulWidget {
  static String routeName = "/home";

  @override
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> with SingleTickerProviderStateMixin{
  final logic = Get.find<HomeLogic>();

  final state = Get.find<HomeLogic>().state;

  late TabController tabController;

  @override
  void initState() {
    tabController = TabController(length: 6, vsync: this, initialIndex: 1);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeLogic>(builder: (logic) {
      return SafeArea(
        child: NestedScrollView(
          headerSliverBuilder: (ctx, inner) {
            return [
              SliverToBoxAdapter(
                child: buildHomeHeader(),
              ),
              SliverAppBar(
                backgroundColor: AppTheme.norWhite,
                title: buildHomeTabBar(),
                pinned: true,
                floating: false,
                snap: false,
                elevation: 0,
              )
            ];
          },
          body: Text("data"),
        ),
      );
    });
  }

  ///TabBar
  Widget buildHomeTabBar() {
    return TabBar(
      controller: tabController,
      tabs: [
        Tab(text: SR.live.tr,),
        Tab(text: SR.recommend.tr,),
        Tab(text: SR.popular.tr,),
        Tab(text: SR.comic.tr,),
        Tab(text: SR.movie.tr,),
        Tab(text: SR.newJourney.tr,),
      ],
      indicatorColor: AppTheme.norMainThemeColor,
      labelColor: AppTheme.norMainThemeColor,
      unselectedLabelColor: AppTheme.norBlack,
      labelStyle: TextStyle(fontSize: 18.sp, fontWeight: FontWeight.normal, fontFamily: 'bilibiliProjectFonts'),
      unselectedLabelStyle: TextStyle(fontSize: 18.sp, fontWeight: FontWeight.normal, fontFamily: 'bilibiliProjectFonts'),
      isScrollable: true,
      indicatorWeight: 3.r,
      indicatorSize: TabBarIndicatorSize.label,
    );
  }

  ///Header
  Widget buildHomeHeader() {
    return Container(
      padding: EdgeInsets.all(8.r),
      child: Row(
        children: [
          buildHomeHeaderAvatar(),
          10.horizontalSpace,
          Expanded(child: buildHomeHeaderSearch()),
          10.horizontalSpace,
          buildHomeHeaderButtons(),
        ],
      ),
    );
  }

  ///Header 头像
  Widget buildHomeHeaderAvatar() {
    return Container(
      alignment: Alignment.center,
      width: 34.r,
      height: 34.r,
      decoration: BoxDecoration(
          color: AppTheme.greyColor.withOpacity(.2),
          borderRadius: BorderRadius.all(Radius.circular(100.r))),
      child: Text(
        SR.login.tr,
        style: TextStyle(
          fontSize: 14.sp,
          color: AppTheme.norMainThemeColor,
        ),
      ),
    );
  }

  /// Header 搜索
  Widget buildHomeHeaderSearch() {
    TextStyle searchTextStyle = TextStyle(
      fontSize: 10.sp,
      color: AppTheme.norBlack,
    );
    return Container(
      height: 27.r,
      decoration: BoxDecoration(
          color: AppTheme.greyColor.withOpacity(.2),
          borderRadius: BorderRadius.circular(100.r)),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          8.horizontalSpace,
          Image.asset(
            AssetsImage.searchCustomPNG,
            width: 15.r,
            height: 15.r,
          ),
          5.horizontalSpace,
          state.firstSearchKey.isNotEmpty
              ? Text(state.firstSearchKey, style: searchTextStyle)
              : Text(SR.searching.tr, style: searchTextStyle)
        ],
      ),
    );
  }

  ///Header 按钮们
  Widget buildHomeHeaderButtons() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Image.asset(
          AssetsImage.gameCustomPNG,
          width: 25.r,
          height: 25.r,
        ),
        15.horizontalSpace,
        Image.asset(
          AssetsImage.mailCustomPNG,
          width: 25.r,
          height: 25.r,
        ),
      ],
    );
  }
}
